//定义全局变量
var token = selectByKey("like_token");
var orSuccess = true;
var pageNum = 1;
var orRefresh = true, key = 0;
var status = 0;
var mask = document.querySelector(".mask");
var orScroll = false;  //当有数据才可以滚动
function reEnter () {
    document.querySelector('.info').style.display = 'block';
    setTimeout(function() {
        window.location.href = '../personal/enter.html';
    },1500);
}
//循环添加内容
function ajaxHtml(data,sta,id,totalNum,goodId){
    var title, className,listClass;
    switch (sta){
        case 1:
            title = "待收货订单";
            className = "dsh";
            listClass = "dshList";
            break;
        case 2:
            title = "待收货订单";
            className = "dsh";
            listClass = "dshList";
            break;
        case 3:
            title = "已收货订单";
            className = "ysh";
            listClass = "yshList";
            break;
        case 4:
            title = "待付款订单";
            className = "dzf";
            listClass = "dzfList";
        default :
            break;
    }

    var mainHtml = document.querySelector("main");
    var mode = document.getElementById("orderMode");
    var headStr = mode.innerHTML;
    headStr = headStr.replace(/\{\{class\}\}/, className);
    headStr = headStr.replace(/\{\{head\}\}/, title);
    headStr = headStr.replace(/\{\{total_num\}\}/g, totalNum);
    headStr = headStr.replace(/\{\{list_id\}\}/, id);
    headStr = headStr.replace(/\{\{good_id\}\}/, goodId);
    headStr = headStr.replace(/\{\{good_status\}\}/, sta);
    var listMode = document.getElementById("orderListMode");
    var html = mainHtml.innerHTML;
    var modeArr = headStr.split('<div class="btm">');
    var listHtml = '';
    for(var k in data){
        var preHtml = listMode.innerHTML;
        preHtml = preHtml.replace(/\{\{src\}\}/, data[k].good_img);
        preHtml = preHtml.replace(/\{\{name\}\}/g, data[k].good_name);
        preHtml = preHtml.replace(/\{\{price\}\}/, data[k].good_price);
        preHtml = preHtml.replace(/\{\{num\}\}/g, data[k].good_num);
        preHtml = preHtml.replace(/\{\{listClass\}\}/, listClass);
        listHtml += preHtml;
    }
    modeArr.splice(1,0,listHtml);
    modeArr.splice(2,0,'<div class="btm">');
    var modeStr = modeArr.join("");
    html += modeStr;
    mainHtml.innerHTML = html;
    //去掉未发货的确认收货
    var confirmList = document.querySelectorAll(".dsh .confirm");
    for(var i = 0; i < confirmList.length; i++){
        if(confirmList[i].parentNode.parentNode.parentNode.getAttribute("data-status") != 2){
            confirmList[i].style.display = "none";
            confirmList[i].nextSibling.style.marginRight = "15px";
        }
    }
}
function search(pages,sta){
    var main = document.querySelector("main");
    mask.style.display = "block";
    $.ajax({
        url:pathName+"/api/v1/member/order?page="+pages+"&status="+sta,
        type:"GET",
        headers:{
            "accept":"application/json",
            "Authorization":"Bearer "+token
        },
        success:function(res){
            console.log(res);
            orScroll = true;
            mask.style.display = "none";
            orSuccess = true;
            if(res.last_page > res.current_page){
                pageNum = parseInt(res.current_page)+1;
            }else{
                orRefresh = false;
            }
            var hadNone = document.getElementById("none");
            if(res.data.countGoods == undefined || res.data.countGoods >= 1){
                main.style.display = "block";
                hadNone.style.display = "none";
                var resData = res.data;
                for(var k in resData){
                    if(k != "countGoods"){
                        ajaxHtml(resData[k].goodDetail,resData[k].status,resData[k].order_id,resData[k]["countGoods"],resData[k].id);
                    }
                }
            }else{
                hadNone.style.display = "block";
                main.style.display = "none";
            }
            operate();
        },
        error:function(){
            if(!token){
                reEnter();
            }
        }
    })
}
function s() {
    var spans = document.getElementById('select').children;
    var main = document.querySelector("main");
    for (var i = 0; i < spans.length; i++) {
        spans[i].index = i;
        Tap(spans[i], function (e, dom) {
            pageNum = 1;
            key = 0;
            orRefresh = true;
            orScroll = false;
            if(orSuccess){
                orSuccess = false;
                main.innerHTML = "";
                main.style.display = "none";
                status = dom.index;
                if(dom.index == 1){
                    status = 2;
                }else if(dom.index == 2){
                    status = 1;
                }
                search(1,status);
                for (var o = 0; o < spans.length; o++) {
                    spans[o].className = '';
                }
                dom.className = 'bblack';
            }
        })
    }
}

function operate(){
    var services = document.getElementsByClassName("service");
    var lists = document.getElementsByClassName("list");
    var payNows = document.getElementsByClassName("payNow");
    var confirms = document.getElementsByClassName("confirm");
    for(var m = 0; m < confirms.length; m++){
        Tap(confirms[m],function(e,dom){
            var goodId = dom.parentNode.parentNode.parentNode.getAttribute("data-goodsid");
            var listId = dom.parentNode.parentNode.parentNode.getAttribute("data-list_id");
            var markT = document.querySelector(".markT");
            var sureT = markT.querySelector("#sureT");
            var cancleT = markT.querySelector("#cancleT")
            markT.style.display = "block";
            Tap(sureT,function(){
                $.ajax({
                    url:pathName+"/api/v1/member/order/confirm/"+goodId,
                    type:"GET",
                    headers:{
                        "Accept":"application/json",
                        "Authorization":"Bearer "+token
                    },
                    success:function(data){
                        if(data == 1){
                            window.location.href = "success.html?id="+listId;
                        }
                    },
                    error:function(){
                        if(!token){
                            reEnter();
                        }
                    }
                })
            })
            Tap(cancleT,function(){
                markT.style.display = "none";
            })
        })
    }
    for(var k = 0; k < payNows.length; k++){
        Tap(payNows[k],function(e,dom){
            var good_id = dom.parentNode.parentNode.parentNode.getAttribute("data-list_id");
            window.location.href = "payorder.html?id="+good_id;
        })
    }
    for(var i = 0; i < services.length; i++){
        Tap(services[i],function(){
            window.location.href = "../service.html";
        });
    }
    for(var j = 0; j < lists.length; j++){
        Tap(lists[j],function(e,dom){
            var statu = dom.parentNode.parentNode.getAttribute("data-status");
            var listId = dom.parentNode.parentNode.getAttribute("data-list_id");
            switch (statu){
                case "1":
                    window.location.href = "waitOut.html?id="+listId;
                    break;
                case "2":
                    window.location.href = "hadOut.html?id="+listId;
                    break;
                case "3":
                    window.location.href = "success.html?id="+listId;
                    break;
                case "4":
                    window.location.href = "orderdetails.html?id="+listId;
                    break;
                default :
                    break;
            }
        });
    }
}
//顶部4个选择
window.onload = function () {
    s();
    var spans = document.getElementById('select').children;
    var hash = window.location.hash;
    switch (hash){
        case "#waitPay":
            spans[1].className = 'bblack';
            status = 2;
            break;
        case "#waitReceive":
            spans[2].className = 'bblack';
            status = 1;
            break;
        case "#hadReceive":
            spans[3].className = "bblack";
            status = 3;
            break;
        default :
            spans[0].className = 'bblack';
            break;
    }
    search(1,status);
    //滑动加载
    window.onscroll = function () {
        if(orScroll){
            var t = document.documentElement.scrollTop || document.body.scrollTop;
            //滚动过的高度(屏幕顶到最上面··不包括现在的屏幕高度)
            var y = document.documentElement ? document.documentElement.scrollHeight : document.body.scrollHeight;
            //浏览器滚动条总高
            var w = document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight;
            //屏幕总高
            if (t + w > y - 150 ) {
                if (orRefresh && key == 0) {
                    key = 1;
                    search(pageNum,status);
                }
            }
        }
    };
};